<?php

namespace app\admin\model;

use think\Model;

class Activity extends Model
{
    // 表名
    protected $name = 'activity';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'is_house_text',
        'category_text',
        'time_text',
        'create_time_text',
        'update_time_text',
        'category_text2'
    ];
    

    public function getBusinessTime($value)
    {
        $business_time = model('business_time')->where('id',$value)->find();
        return $business_time['name'];
    } 

    public function getCity($value)
    {
        $city = model('city')->where('id',$value)->find();
        return $city['name'];
    } 

    public function getCategoryText2Attr($value, $data=null)
    {
        $value = $value ? $value : $data['category'];
        $courseCat = model('courseCat')->where('id',$value)->find();
        return $courseCat['name'];
    }  

    public function getIsHouseList()
    {
        return ['0' => __('Is_house 0'),'1' => __('Is_house 1')];
    }     


    public function getExtraServiceAttr($value)
    {
        $valueArr = explode(',', $value);
        $list = $this->getExtraService();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }

    public function getExtraService()
    {
        return ['1' => '酒水饮料','2' => '茶点小吃','3' => '餐饮','5' => '投影仪','6' => '音响设备','7' => '白板'];
    } 

    // 获取停车场数量
    public function getPackNumber($value)
    {
        $list = ['0' => '0-50','1' => '50-100','2' => '100-200','3' => '200-500','4' => '500-1000'];
        return isset($list[$value]) ? $list[$value] : '';
    } 

    public function getCategoryList()
    {
        return ['1' => __('Category 1'),'2' => __('Category 2'),'3' => __('Category 3'),'5' => __('Category 5'),'6' => __('Category 6'),'7' => __('Category 7'),'8' => __('Category 8'),'9' => __('Category 9'),'10' => __('Category 10'),'11' => __('Category 11'),'12' => __('Category 12')];
    }     


    public function getIsHouseTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['is_house'];
        $list = $this->getIsHouseList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCategoryTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['category'];
        $list = $this->getCategoryList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['create_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['update_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    public function supplier()
    {
        return $this->belongsTo('Supplier', 'supplier_id')->setEagerlyType(0);
    }

    public function city()
    {
        return $this->belongsTo('City', 'city_id')->setEagerlyType(0);
    }


}
